package command

import (
	"mime/multipart"
	"strings"
	"time"

	"ssadmin/common/constant"
	"ssadmin/common/query"
	"ssadmin/common/utils"
	"ssadmin/internal/admin/models"
)

type FileAddReq struct {
	query.UserInfo `search:"-"`
	File           *multipart.FileHeader `form:"file" comment:""`
	Force          bool                  `form:"force"`
	FilePath       string                `form:"filePath"`
}

func (d *FileAddReq) Generate(fileInfos []utils.FileInfo) []models.Resource {
	items := make([]models.Resource, 0)
	now := time.Now()
	for i := range fileInfos {
		fileInfo := fileInfos[i]
		items = append(items, models.Resource{
			Id:         utils.GetCommUtil().SimpleUUID(),
			Path:       strings.ReplaceAll(fileInfo.Path, constant.FilePath+"/", ""),
			FileSize:   fileInfo.Size,
			FileName:   fileInfo.OriginalName,
			ExtType:    fileInfo.Type,
			Width:      fileInfo.Width,
			Height:     fileInfo.Height,
			Remark:     "",
			AgencyId:   d.AgencyId,
			CreatorId:  &d.UserId,
			Creator:    &d.UserName,
			CreateTime: &now,
			UpdateTime: &now,
		})
	}
	return items
}

type FileEditReq struct {
	query.UserInfo `search:"-"`
	Id             string                 `json:"id" vd:"len($)>0"`
	CategorizeId   *string                `json:"categorizeId"`
	Seq            *int                   `json:"seq"`
	Label          *string                `json:"label"`
	Param          map[string]interface{} `json:"param"`
	ImgId          *string                `json:"imgId"`
	ImgWidth       *int                   `json:"imgWidth"`
	ImgHeight      *int                   `json:"imgHeight"`
	Remark         *string                `json:"remark"`
}

func (d *FileEditReq) GenerateMap() map[string]interface{} {
	// 可编辑项
	editMap := map[string]interface{}{}
	if d.CategorizeId != nil && len(*d.CategorizeId) > 0 {
		editMap["categorize_id"] = *d.CategorizeId
	}
	if d.Seq != nil {
		editMap["seq"] = *d.Seq
	}
	if d.Label != nil && len(*d.Label) > 0 {
		editMap["label"] = *d.Label
	}
	if d.Param != nil {
		editMap["param"] = utils.GetCommUtil().MapToBytes(d.Param)
	}
	if d.ImgId != nil && len(*d.ImgId) > 0 {
		editMap["img_id"] = *d.ImgId
	}
	if d.ImgWidth != nil {
		editMap["img_width"] = *d.ImgWidth
	}
	if d.ImgHeight != nil {
		editMap["img_height"] = *d.ImgHeight
	}
	if d.Remark != nil {
		editMap["remark"] = *d.Remark
	}
	return editMap
}

type FileDeleteReq struct {
	Id string `json:"id" vd:"len($)>0"`
}
